package com.jy.api.service.impl;

import com.jy.api.domain.Product;
import com.jy.api.domain.ProductMaterial;
import com.jy.api.domain.ProductStatus;
import com.jy.api.service.OpenSearchService;
import com.jy.api.util.OpenSearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2018/2/26.
 */
@Slf4j
@Service
public class OpenSearchServiceImpl implements OpenSearchService {

    private static final String appName = "hxy_opensearch";
    private static final String tableName = "product";
    private static final  String materialName = "hxy_member";
    private static final String materialTableName = "material";
    @Async
    @Override
    public void openSearchProductPush(Product product) {

        /**
         * 异步同步商品信息
         */
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("id", product.getId());
            map.put("store_id", product.getStoreId());
            map.put("name", product.getName());
            if (product.getProductStatus() == ProductStatus.OFF) {
                map.put("product_status", "1");
            } else {
                map.put("product_status", "0");
            }

            map.put("is_enabled", product.isEnabled());

            OpenSearchUtil.push(map, appName, tableName);
        } catch (Exception ex) {
            log.info("openSearchUtil push is error:{}", ex);
        }

    }

    @Async
    @Override
    public void openSearchMaterialPush(ProductMaterial product) {
        /**
         * 异步同步商品信息
         */
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("id", product.getId());
            map.put("store_id", product.getStoreId());
            map.put("name", product.getName());
            OpenSearchUtil.push(map, materialName, materialTableName);
        } catch (Exception ex) {
            log.info("openSearchUtil push is error:{}", ex);
        }
    }



}
